###
# Usage: Rscript cell_rel_cohort_cumulativePercent.R
###
library(reshape2)
library(dplyr)
library(ggplot2)
library(RColorBrewer)

library(optparse)

option_list <- list(
  make_option("--b", default = "", type = "character", help = "blood mean file"),
  make_option("--t", default = "", type = "character", help = "tissue mean file"),
  make_option("--bo", default = "", type = "character", help = "blood output png file"),
  make_option("--to", default = "", type = "character", help = "tissue output png file")
)
opt <- parse_args(OptionParser(option_list = option_list))

# path = 'D:\\exo\\889\\submit 5.20\\cell_rel'
# setwd(path)

file.blood = opt$b
file.tissue =  opt$t
file.out.blood = opt$bo
file.out.tissue = opt$to
w=7
h=5

### blood cells
data.cells = read.csv(file.blood, sep=',', header = TRUE, row.names=1)
data = data.cells * 100
data = as.data.frame(t(data))
data_rownames = rownames(data)
data_colnames = colnames(data)
data$`Blood cell` = data_rownames
data_m = melt(data, id.vars=c("Blood cell"))
cohortOrder = c("Urine","CSF","Bile","Healthy","Benign","BRCA","CHD","CRC","ESCC","GBM","GC","HCC","KIRC","ML","MEL","OV","PAAD","SCLC")
data_m$variable = factor(data_m$variable, levels = cohortOrder)

pdf(file.out.blood, width=w, height=h)
colors = c(brewer.pal(11,"RdYlBu"), brewer.pal(11,"PiYG"), '#003300')
p = ggplot(data_m, aes(x=variable, y=value)) +
  geom_bar(stat="identity", position="fill", aes(fill=`Blood cell`), width = 0.7) +  
  scale_fill_manual(values=colors)+
  scale_y_continuous(labels = scales::percent)+
  labs(x = "", y = 'Relative Abundance', title = "The Relative Abundance of Cohorts")+
  guides(fill = guide_legend(ncol = 5, bycol = TRUE, override.aes = list(size = 5))) +
  theme(plot.title = element_text(hjust = 0.5), axis.title = element_text(size = 16, face = "bold",color = 'black'))+
  theme(axis.title.y = element_text(face = 'bold',color = 'black',size = 14),
        axis.title.x = element_text(face = 'bold',color = 'black',size = 14,vjust = -1.2),
        axis.text.y = element_text(face = 'bold',color = 'black',size = 10, angle = 360),
        axis.text.x = element_text(face = 'bold',color = 'black',size = 10, angle = 45, hjust = 1), 
        panel.grid = element_blank(),
        legend.position = 'top',
        legend.key.height = unit(0.5,'cm'),
        legend.text = element_text(face = 'bold',color = 'black',size = 9))
print(p)
dev.off()


### tissue cells
data.cells = read.csv(file.tissue, sep=',', header = TRUE, row.names=1)
data = data.cells * 100
data = as.data.frame(t(data))
data_rownames = rownames(data)
data_colnames = colnames(data)
data$`Tissue cell` = data_rownames
data_m = melt(data, id.vars=c("Tissue cell"))
cohortOrder = c("Urine","CSF","Bile","Healthy","Benign","BRCA","CHD","CRC","ESCC","GBM","GC","HCC","KIRC","ML","MEL","OV","PAAD","SCLC")
data_m$variable = factor(data_m$variable, levels = cohortOrder)

pdf(file.out.tissue, width=w, height=h)
colors = c(brewer.pal(11,"Spectral"), brewer.pal(5,"PRGn"))
p = ggplot(data_m, aes(x=variable, y=value)) +
  geom_bar(stat="identity", position="fill", aes(fill=`Tissue cell`), width = 0.7) +  
  scale_fill_manual(values=colors)+
  scale_y_continuous(labels = scales::percent)+
  labs(x = "", y = 'Relative Abundance', title = "The Relative Abundance of Cohorts")+
  guides(fill = guide_legend(ncol = 5, bycol = TRUE, override.aes = list(size = 5))) + 
  theme(plot.title = element_text(hjust = 0.5), axis.title = element_text(size = 16, face = "bold",color = 'black'))+
  theme(axis.title.y = element_text(face = 'bold',color = 'black',size = 14),
        axis.title.x = element_text(face = 'bold',color = 'black',size = 14,vjust = -1.2),
        axis.text.y = element_text(face = 'bold',color = 'black',size = 10),
        axis.text.x = element_text(face = 'bold',color = 'black',size = 10,angle = 45,hjust = 1), 
        panel.grid = element_blank(),
        legend.position = 'top',
        legend.key.height = unit(0.5,'cm'),
        legend.text = element_text(face = 'bold',color = 'black',size = 9))
print(p)
dev.off()

















